#!/usr/bin/python
#coding=utf-8


import os, sys
import socket
import time
import threading
import thread

class CommThread(threading.Thread):
	def __init__(self, host, port):
			threading.Thread.__init__(self)
			self.host = host
			self.port = port
	
	def run(self):
		print "start one thread " 
		clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		clisock.connect((self.host, self.port))
		
		sendstr = "ni ge nnd "
		clisock.send(sendstr)
		recvstr = clisock.recv(len(sendstr))
		print "recvstr :%s"%(recvstr)

def comm_thread(host, port):
	print "nnd"
	try:
		clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		clisock.connect((host,port))
	
		sendstr = "ni ge nnd "
		clisock.send(sendstr)
		recvstr = clisock.recv(len(sendstr))
		print "recvstr :%s"%(recvstr)
	except Exception, ex:
		print "in comm_thread(), exception: ", ex




def test(num, host, port):
	for i in xrange(num):
		try:
			#commthread = CommThread(host, port)
			t1 = time.time()
			address = (host, port)
			thread.start_new_thread(comm_thread, (host, port))
			#commthread.run()
			t2 = time.time()
			print "t2-t1 ", t2-t1
		except Exception, ex:
			print "in test(), exception: %s"%(str(ex))

if __name__=="__main__":
	if len(sys.argv) != 4:
		print "Usage test_tcp_server 100 127.0.0.1 9800 "
		sys.exit(1)
	test(int(sys.argv[1]), sys.argv[2], int(sys.argv[3]))


	
	
	
